layui.use(['layer', 'table', 'admin', 'ax', 'dropdown'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var dropdown = layui.dropdown;


    /**
     * 定时任务管理初始化
     */
    var SysJob = {
        tableId: "SysJobTable",	//表格id
        condition: {}
    };

    /**
     * 初始化表格的列
     */
    SysJob.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {title: '任务名称', field: 'jobName', visible: true, align: 'center', valign: 'middle'},
            {title: '任务组名', field: 'jobGroup', visible: true, align: 'center', valign: 'middle'},
            {title: '目标字符串', field: 'invokeTarget', visible: true, align: 'center', valign: 'middle', templet:function (data) {
                    return '<span lay-tips="' + data.invokeTarget + '" style="color: #337ab7">data.invokeTarget</span>';
                }},
            {title: '执行表达式', field: 'cronExpression', visible: true, align: 'center', valign: 'middle'},
            {title: '状态',field: 'status',visible: true,align: 'center', valign: 'middle',templet: function (data) {
                    if (data.status == 0) {
                        return '正常';
                    } else {
                        return '暂停';
                    }
                }
            },
            {title: '创建时间', field: 'createTime', visible: true, align: 'center', valign: 'middle'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 220}
        ]];
    };


    /**
     * 点击添加定时任务
     */
    SysJob.openAddSysJob = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            area: '600px',
            title: '添加定时任务',
            content: Feng.ctxPath + '/sysJob/sysJob_add',
            end: function () {
                admin.getTempData('formOk') && table.reload(SysJob.tableId);
            }
        });
    };

    /**
     * 点击修改
     * @param data
     */
    SysJob.onEditSysJob = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            area: '600px',
            title: '修改',
            content: Feng.ctxPath + '/sysJob/sysJob_edit?sysJobId=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(SysJob.tableId);
            }
        });
    };

    /**
     * 打开查看定时任务详情
     */
    SysJob.openSysJobDetail = function (data) {
        top.layui.admin.open({
            type: 2,
            area: '600px',
            title: '详情',
            content: Feng.ctxPath + '/sysJob/sysJob_info?sysJobId=' + data.id,
            end: function () {
            }
        });
    };

    /**
     * 删除定时任务
     */
    SysJob.delete = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/sysJob/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(SysJob.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            }, true);
            ajax.set("sysJobId", data.id);
            ajax.start();
        }
        Feng.confirm("是否删除定时任务" + data.jobName + "?", operation);
    };

    /**
     * 打开查看定时任务log日志
     */
    SysJob.showJobLogDialog = function (data) {
        layui.admin.open({
                type: 2,
                title: '定时任务日志',
                content: Feng.ctxPath + '/sysJobLog?sysJobId=' + data.id,
                success: function(layero, index){
                    layer.full(index);
                }
            });
    };


    /**
     * 查询定时任务列表
     */
    SysJob.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(SysJob.tableId, {where: queryData});
    };

    /**
     * 执行任务
     */
    SysJob.run = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/sysJob/run", function (data) {
                if (data == 200) {
                    Feng.success("任务执行成功!");
                } else {
                    Feng.error(data.message)
                }
            }, function (data) {
                Feng.error("错误!" + data.responseJSON.message + "!");
            }, true);
            ajax.set("id", data.id);
            ajax.start();
        }
        Feng.confirm("确认要立即执行一次任务吗?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + SysJob.tableId,
        url: Feng.ctxPath + '/sysJob/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        toolbar: "#toolbar",
        cols: SysJob.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        SysJob.search();
    });


    //头工具栏事件
    table.on('toolbar(' + SysJob.tableId + ')', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'btnAdd':
                SysJob.openAddSysJob();
                break;
        };
    });


    // 工具条点击事件
    table.on('tool(' + SysJob.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            SysJob.onEditSysJob(data);
        } else if (layEvent === 'delete') {
            SysJob.delete(data);
        } else if (layEvent === 'info') {
            SysJob.openSysJobDetail(data);
        } else if (layEvent === 'run') {
            SysJob.run(data);
        } else if (layEvent === 'jobLog') {
            SysJob.showJobLogDialog(data);
        }


    });
});

